package com.java247;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        // 创建集合对象
        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = s1.getAge() - s2.getAge();
                int newNum = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
                return newNum;
            }
        });

        // 创建学生对象
        Student s1 = new Student("a", 1);
        Student s2 = new Student("b", 2);
        Student s3 = new Student("c", 1);

        // 添加元素
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);

        // 遍历
        for (Student s : ts) {
            System.out.println(s.getName() + ':' + s.getAge());
        }
    }
}
